文档

使用Terraform管理SASL用户

更新时间:

云消息队列 Kafka 版实例的默认SASL用户仅提供身份校验,支持所有Topic和Group的读写权限。如果需要更细致的权限控制,您需开启ACL,创建SASL用户,按需赋予SASL用户向云消息队列 Kafka 版收发消息的权限。使用Terraform的alicloud_alikafka_sasl_user资源可以创建和管理SASL用户。

前提条件

  • 安装Terraform。支持Terraform 0.13及以上版本。

  • 配置阿里云账号信息。

    选择一种阿里云认证方式,为Terraform的执行提供认证信息。本文以环境变量认证方式为例:

    export ALICLOUD_ACCESS_KEY="************"
    export ALICLOUD_SECRET_KEY="************"
    export ALICLOUD_REGION="cn-hangzhou"
    说明

    为保障数据安全性,建议您按需为RAM用户授予Kafka资源的操作权限。具体操作,请参见RAM主子账号授权

背景信息

Terraform的alicloud_alikafka_sasl_user资源提供了以下参数:

  • (必填)instance_id:实例ID。

  • (必填)username:SASL用户名称。必须以字母开头,长度限制在3~64个字符之间,只能包含字母、数字、短划线(-)、下划线(_)。

  • (可选)password:SASL用户密码。此参数和kms_encrypted_password须至少选择其中一个。

  • (可选)kms_encrypted_password:Kms密钥。

  • (可选)kms_encryption_context:Kms加密上下文。

  • (可选)type:用户类型。默认为plain

    • plain:一种简单的用户名密码校验机制。云消息队列 Kafka 版优化了PLAIN机制,支持不重启实例的情况下动态增加SASL用户。

    • scram:一种用户名密码校验机制,安全性比PLAIN更高。云消息队列 Kafka 版使用 SCRAM-SHA-256。

更多信息,请参见alicloud_alikafka_sasl_user

创建SASL用户

说明

实例类型为专业版并且已开启ACL的实例才可以创建SASL用户。

本示例以在华东1(杭州)地域下创建SASL用户为例。

  1. 创建一个用于存放Terraform资源的项目文件夹,命名为terraform。

  2. 执行以下命令,进入项目目录。

    cd terraform
  3. 执行以下命令,创建名为sasl.tf的配置文件。

    resource "alicloud_alikafka_sasl_user" "default" {
      instance_id = "alikafka_post-cn-uax3go6z****"
      username    = var.name
      password    = var.password
    }
    
    
    variable "name" {
      description = "Name Description"
      type     = string
    }
    
    variable "password" {
      description = "Password"
      type     = string
    }
    
    
    output "name" {
      value = var.name
      description = "Name Description"
    }
  4. 执行以下命令,初始化Terraform运行环境。

    terraform init

    预期输出:

    Initializing the backend...
    
    Initializing provider plugins...
    - Reusing previous version of hashicorp/alicloud from the dependency lock file
    - Using previously-installed hashicorp/alicloud v1.211.2
    
    ...
    
    Terraform has been successfully initialized!
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  5. 依次执行以下命令,创建SASL用户。

    1. 执行以下命令,执行配置文件。

      terraform apply
    2. 根据提示依次输入SASL用户名称、密码等信息。

      预期输出:

      ...
      
      Plan: 1 to add, 0 to change, 0 to destroy.
      
      Changes to Outputs:
        + name = "test_user"
      
      ...
      
      alicloud_alikafka_sasl_user.default: Creating...
      alicloud_alikafka_sasl_user.default: Creation complete after 3s [id=alikafka_post-cn-uax3go6z****:test_user]
      
      Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
      
      Outputs:
      
      name = "test_user"

    SASL用户已成功创建。

删除SASL用户

  1. 在目标项目目录内执行以下命令,运行配置文件。

    terraform destroy
  2. 根据提示依次输入SASL用户名称、密码等信息。

    预期输出:

    alicloud_alikafka_sasl_user.default: Refreshing state... [id=alikafka_post-cn-uax3go6z****:test_user]
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      - destroy
    
    Terraform will perform the following actions:
    
      # alicloud_alikafka_sasl_user.default will be destroyed
      - resource "alicloud_alikafka_sasl_user" "default" {
          - id          = "alikafka_post-cn-uax3go6z****:test_user" -> null
          - instance_id = "alikafka_post-cn-uax3go6z****" -> null
          - password    = (sensitive value)
          - type        = "plain" -> null
          - username    = "test_user" -> null
        }
    
    Plan: 0 to add, 0 to change, 1 to destroy.
    
    Changes to Outputs:
      - name = "test_user" -> null
    
    ...
    
    alicloud_alikafka_sasl_user.default: Destroying... [id=alikafka_post-cn-uax3go6z****:test_user]
    alicloud_alikafka_sasl_user.default: Destruction complete after 1s
    
    Destroy complete! Resources: 1 destroyed.

    SASL用户已成功删除。

查询SASL用户

  1. 创建一个用于存放Terraform资源的项目文件夹,命名为terraform。

  2. 执行以下命令,进入项目目录。

    cd terraform
  3. 执行以下命令,创建名为main.tf的配置文件。

    data "alicloud_alikafka_sasl_users" "sasl_users_ds" {
      instance_id = "<Kafka实例ID>"
      output_file = "saslUsers.txt"
    }
    
    output "first_sasl_username" {
      # # 输出列表中的第一个SASL用户的名称。请注意,索引[0]在这里表示列表中的第一项。
      value = "${data.alicloud_alikafka_sasl_users.sasl_users_ds.users.0.username}"
    }
  4. 执行以下命令,初始化Terraform运行环境。

    terraform init

    预期输出:

    Initializing provider plugins...
    - Reusing previous version of hashicorp/alicloud from the dependency lock file
    - Using previously-installed hashicorp/alicloud v1.211.2
    
    Terraform has been successfully initialized!
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  5. 执行以下命令,查询实例列表中第一个SASL用户。

    terraform apply

    预期输出:

    You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
    
    ...
    
    Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
    
    Outputs:
    
    first_sasl_username = "wbtes****"

    若需获取SASL用户的密码信息,请在saslUsers.txt文件中查看。

    [
    	{
    		"password": "******",
    		"username": "wbtes****"
    	}
    ]

相关文档

  • 本页导读 (1)